package plugins

import (
	"html/template"
	"io"

	"github.com/labstack/echo/v4"
	"github.com/quarkcloudio/quark-go/v2/pkg/builder"
)

type Template struct {
	templates *template.Template
}

func (p *Template) CreateTemplateRender(b *builder.Engine) {
	b.Echo().Renderer = &Template{
		templates: template.Must(template.ParseGlob("web/template/*.html")),
	}
}

// 模板渲染方法
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
	if viewContext, isMap := data.(map[string]interface{}); isMap {
		viewContext["reverse"] = c.Echo().Reverse
	}

	return t.templates.ExecuteTemplate(w, name, data)
}
